package com.zhiche.lisa.bms.service.waybill.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zhiche.lisa.bms.dao.mapper.waybill.WaybillStateMapper;
import com.zhiche.lisa.bms.dao.model.waybill.WaybillState;
import com.zhiche.lisa.bms.enums.WaybillStateEnum;
import com.zhiche.lisa.bms.service.waybill.WaybillStateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * 运单状态同步表 服务实现类
 * </p>
 *
 * @author liuanshun
 * @since 2018-12-07
 */
@Service
public class WaybillStateServiceImpl extends ServiceImpl<WaybillStateMapper, WaybillState> implements WaybillStateService {
    /**
     * 根据指令号获取运单状态同步表
     *
     * @param attribute16
     * @return
     */
    @Override
    public List<WaybillState> selectWaybillStateByAttribute(String attribute16) {
        EntityWrapper<WaybillState> waybillStateEntityWrapper = new EntityWrapper<>();
        waybillStateEntityWrapper.eq("attribute16", attribute16);
        return this.selectList(waybillStateEntityWrapper);
    }

    @Override
    public List<WaybillState> selectWaybillStateByAttributes(List<String> attribute16) {
        EntityWrapper<WaybillState> waybillStateEntityWrapper = new EntityWrapper<>();
        waybillStateEntityWrapper.in("attribute16", attribute16);
        return this.selectList(waybillStateEntityWrapper);
    }

    /**
     * 根据系统运单号获取运单状态同步表<type>单个</type>
     *
     * @param ordeReleaseGid
     * @return
     */
    @Override
    public List<WaybillState> selectWaybillStateByOdrdeReleaseGid(String ordeReleaseGid) {
        EntityWrapper<WaybillState> waybillStateEntityWrapper = new EntityWrapper<>();
        waybillStateEntityWrapper.eq("order_release_gid", ordeReleaseGid);
        return this.selectList(waybillStateEntityWrapper);
    }

    /**
     * 根据系统运单号获取运单状态同步表<type>批量</type>
     *
     * @param ordeReleaseGids
     * @return
     */
    @Override
    public List<WaybillState> selectWaybillStateByOdrdeReleaseGids(List<String> ordeReleaseGids) {
        EntityWrapper<WaybillState> waybillStateEntityWrapper = new EntityWrapper<>();
        waybillStateEntityWrapper.in("order_release_gid", ordeReleaseGids);
        return this.selectList(waybillStateEntityWrapper);
    }

    /**
     * 查询通过验证的状态同步表
     *
     * @param attribute16
     * @return
     */
    @Override
    public List<WaybillState> selectCheckedWaybillStateByAttribute(String attribute16) {
        List<WaybillState> returnVal = new ArrayList<>();
        List<WaybillState> waybillStates = this.selectWaybillStateByAttribute(attribute16);
        if (!CollectionUtils.isEmpty(waybillStates)) {
            waybillStates.forEach(waybillState -> {
                if (StringUtils.isEmpty(waybillState.getPodStatus())) {
                    if (checkWaybillStateDescription(waybillState)) {
                        returnVal.add(waybillState);
                    }
                } else {
                    returnVal.add(waybillState);
                }
                // returnVal.add(waybillState);
            });
        }
        return returnVal;
    }

    @Override
    public List<WaybillState> selectCheckedWaybillStateByAttributes(List<String> attribute16) {
        List<WaybillState> returnVal = new ArrayList<>();
        List<WaybillState> waybillStates = this.selectWaybillStateByAttributes(attribute16);
        if (!CollectionUtils.isEmpty(waybillStates)) {
            waybillStates.forEach(waybillState -> {
                if (StringUtils.isEmpty(waybillState.getPodStatus())) {
                    if (checkWaybillStateDescription(waybillState)) {
                        returnVal.add(waybillState);
                    }
                } else {
                    returnVal.add(waybillState);
                }
                // returnVal.add(waybillState);
            });
        }
        return returnVal;
    }

    @Override
    public List<String> selectCheckedAttribute(List<String> attribute16) {
        Set<String> returnVal = new HashSet<>();
        if (CollectionUtils.isEmpty(attribute16)) {
            return new ArrayList<>(returnVal);
        }
        List<WaybillState> waybillStates = this.selectWaybillStateByAttributes(attribute16);
        // if (!CollectionUtils.isEmpty(waybillStates)) {
        //     waybillStates.forEach(waybillState -> {
        //         // if (StringUtils.isEmpty(waybillState.getPodStatus())) {
        //         //     if (checkWaybillStateDescription(waybillState)) {
        //         //         returnVal.add(waybillState.getAttribute16());
        //         //     }
        //         // } else {
        //         //     returnVal.add(waybillState.getAttribute16());
        //         // }
        //         if (isReturnFlagWaybill(waybillState)) {
        //             returnVal.add(waybillState.getAttribute16());
        //         }
        //     });
        // }
        returnVal = verifyWaybillStateByOrderReaid(waybillStates, false);
        return new ArrayList<>(returnVal);
    }

    @Override
    public List<String> selectCheckedOrderReleaseGid(List<String> ordeReleaseGids) {
//        order_release_gid
        Set<String> returnVal = new HashSet<>();
        if (CollectionUtils.isEmpty(ordeReleaseGids)) {
            return new ArrayList<>(returnVal);
        }
        List<WaybillState> waybillStates = this.selectWaybillStateByOdrdeReleaseGids(ordeReleaseGids);
        // if (!CollectionUtils.isEmpty(waybillStates)) {
        //     waybillStates.forEach(waybillState -> {
        //         // if (StringUtils.isEmpty(waybillState.getPodStatus())) {
        //         //     if (checkWaybillStateDescription(waybillState)) {
        //         //         returnVal.add(waybillState.getOrderReleaseGid());
        //         //     }
        //         // } else {
        //         //     returnVal.add(waybillState.getOrderReleaseGid());
        //         // }
        //         if (isReturnFlagWaybill(waybillState)) {
        //             returnVal.add(waybillState.getOrderReleaseGid());
        //         }
        //     });
        // }
        returnVal = verifyWaybillStateByOrderReaid(waybillStates, true);
        return new ArrayList<>(returnVal);
    }

    private static Map<String, String> DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING = new HashMap<>();

    {
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("多位板直发中转", "已入库");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("人送", "已回单");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("平板直送", "已回单");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("水运", "已入库");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("多位板集拼直发", "已入库");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("多位板集拼中转", "已入库");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("铁运直送", "已入库");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("铁运集拼", "已入库");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("铁运集拼中转", "已入库");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("铁运中转集拼", "已入库");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("铁运中转集拼中转", "已入库");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("铁运中转直送", "已入库");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("直送", "已回单");
        DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.put("人送直送", "已回单");
    }

    //    private Boolean checkWaybillStateDescription(WaybillState waybillState) {
//        if (waybillState == null || waybillState.getDescription() == null || waybillState.getRouteModeName() == null) {
//            return null;
//        }
//        if (DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.containsKey(waybillState.getRouteModeName())) {
//            String description = DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.get(waybillState.getRouteModeName());
//            return description.equals(waybillState.getDescription());
//        }
//        return null;
//    }
    private boolean checkWaybillStateDescription(WaybillState waybillState) {
        if (!(waybillState == null || waybillState.getDescription() == null || waybillState.getRouteModeName() == null)) {
            if (DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.containsKey(waybillState.getRouteModeName())) {
                String description = DESCRIPTION2ROUTEMODENAME_CHECK_MAPPING.get(waybillState.getRouteModeName());
                return description.equals(waybillState.getDescription());
            }
        }
        return false;
    }

    /**
     * 根据运单状态集合判断并返回获取
     *
     * @param waybillStateList
     * @param isReleaseGid
     * @return
     */
    private Set<String> verifyWaybillStateByOrderReaid(List<WaybillState> waybillStateList, boolean isReleaseGid) {
        Set<String> resultSet = new HashSet<>();
        if (!CollectionUtils.isEmpty(waybillStateList)) {
            waybillStateList.forEach(waybillState -> {
                if (isReturnFlagWaybill(waybillState)) {
                    if (isReleaseGid) {
                        resultSet.add(waybillState.getOrderReleaseGid());
                    } else {
                        resultSet.add(waybillState.getAttribute16());
                    }
                }
            });
        }
        return resultSet;
    }

    /**
     * 运单状态的判断
     *
     * @param waybillState
     * @return
     */
    private boolean isReturnFlagWaybill(WaybillState waybillState) {
        boolean returnFlag = false;
        if (WaybillStateEnum.ATTRIBUTE_3.getCode().equals(waybillState.getAttribute3())) {
            // attribute3 == 前置库订单
            if (StringUtils.isNotBlank(waybillState.getDestLocationGid())
                    && waybillState.getDestLocationGid().contains(WaybillStateEnum.DEST_LOCATION_GID.getCode())) {
                if (WaybillStateEnum.DESCRIPTION_PUT_STORAGE.getCode().equals(waybillState.getDescription())
                        && WaybillStateEnum.getPodStatusList().contains(waybillState.getPodStatus())) {
                    returnFlag = true;
                }
            } else {
                if ((WaybillStateEnum.DESCRIPTION_ARRIVE.getCode().equals(waybillState.getDescription())
                        || WaybillStateEnum.DESCRIPTION_DELIVER.getCode().equals(waybillState.getDescription())
                        || WaybillStateEnum.DESCRIPTION_RECEIPT.getCode().equals(waybillState.getDescription()))
                        && WaybillStateEnum.getPodStatusList().contains(waybillState.getPodStatus())) {
                    returnFlag = true;
                }
            }
        } else {
            // attribute3 != 前置库订单
            if (StringUtils.isNotBlank(waybillState.getOmsDest()) && StringUtils.isNotBlank(waybillState.getDestLocationGid())
                    && waybillState.getOmsDest().equals(waybillState.getDestLocationGid())) {
                // oms_dest == dest_location_gid
                if ((WaybillStateEnum.DESCRIPTION_ARRIVE.getCode().equals(waybillState.getDescription())
                        || WaybillStateEnum.DESCRIPTION_DELIVER.getCode().equals(waybillState.getDescription())
                        || WaybillStateEnum.DESCRIPTION_RECEIPT.getCode().equals(waybillState.getDescription()))
                        && WaybillStateEnum.getPodStatusList().contains(waybillState.getPodStatus())) {
                    // description in ('已运抵', '已交付') && pod_status in ('无回单', '已回单', '已交单')
                    returnFlag = true;
                }
            } else {
                if (WaybillStateEnum.DESCRIPTION_PUT_STORAGE.getCode().equals(waybillState.getDescription())) {
                    // description == 已入库
                    returnFlag = true;
                }
            }
        }
        return returnFlag;
    }
}
